package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;

import org.apache.log4j.Logger;

import core.corewa.Event;
import db.dao.DictionariesDAO;
import db.dao.LanguagesDAO;
import db.dao.WordsDAO;
import db.dto.DictionariesDTO;
import db.dto.LanguagesDTO;
import db.dto.WordsDTO;
import form.BaseForm;
import form.ReviewerCheckingForm;

@SuppressWarnings("serial")
public class ReviewerHomeController extends BaseController {
	Logger LOG = Logger.getLogger("ReviewerHomeController");
	/**
	 * do nothing in here
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procHome(ActionRequest request, ActionResponse response, Event event) throws PortletException, IOException {
		return null;
	}
	/**
	 * function return for ReviewerChecking page
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procCheckSuggestion(ActionRequest request, ActionResponse response, Event event) throws PortletException, IOException {
		int dictId = Integer.parseInt(request.getParameter("dictId"));
		WordsDAO wordDAO = new WordsDAO();
		List<WordsDTO> listWord = wordDAO.getWord(dictId);
		ReviewerCheckingForm reviewerCheckingForm = new ReviewerCheckingForm();
		List<WordsDTO> listNewWords = new ArrayList<WordsDTO>();
		for (WordsDTO wordsDTO : listWord) {
			if(wordsDTO.getStatus() == 0) {
				listNewWords.add(wordsDTO);
			}
		}
		DictionariesDAO dictDAO = new DictionariesDAO();
		DictionariesDTO dictDTO = dictDAO.getDictionary(dictId);
		LanguagesDAO lanDAO = new LanguagesDAO();
		LanguagesDTO from;
		LanguagesDTO to;
		int fromId = dictDTO.getFrom();
		int toId = dictDTO.getTo();
		from = lanDAO.getLanguage(fromId);
		to = lanDAO.getLanguage(toId);
		reviewerCheckingForm.setDictName(from.getName()+" - "+to.getName());
		reviewerCheckingForm.setListNewWords(listNewWords);
		reviewerCheckingForm.setDictId(dictId);
		return reviewerCheckingForm;
	}
	
	/**
	 * function logout
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procLogout(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		request.getPortletSession().removeAttribute("username", PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().removeAttribute("role", PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().setAttribute("username", "Guest",PortletSession.APPLICATION_SCOPE);
    	request.getPortletSession().setAttribute("role", -1, PortletSession.APPLICATION_SCOPE);
		return null;
	}
}
